#include "head.h"
#include <asm-generic/errno.h>
#include <complex.h>
#include <mysql/mysql.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>

void snd_cmd(int net_fd,command_t* command,MYSQL* mysql){
    char server_path[512] = {"/home/luguanbo/database/"};//这里填服务器根路径path

    int user_id = 0;
    recv(net_fd,&user_id,sizeof(int),MSG_WAITALL);

    char buf1[256] = {0};//sha1sum
    char buf2[256] = {0};//md5
    recv(net_fd,buf1,40,MSG_WAITALL);
    recv(net_fd,buf2,32,MSG_WAITALL);

    int clientfile_len;
    recv(net_fd,&clientfile_len,sizeof(int),MSG_WAITALL);


    char client_path[60] = {0};
    int path_len = 0;
    recv(net_fd,&path_len,sizeof(int),MSG_WAITALL);
    recv(net_fd,client_path,path_len,MSG_WAITALL);

    int name_len;
    recv(net_fd,&name_len,sizeof(int),MSG_WAITALL);
    char file_name[60] = {0};
    recv(net_fd,file_name,name_len,MSG_WAITALL);

    strcat(server_path,buf1);//得到数据库存放所有文件的地址
    
    char user_path[60] = {0};
    strcpy(user_path,client_path);

    
    strcat(client_path,"/");
    strcat(client_path,file_name);


    char sql_buf[1024] = {0};
    sprintf(sql_buf,
            "select * from file where path =\"%s\"",
            client_path);
    if (mysql_query(mysql, sql_buf)) {
        printf("%s\n", mysql_error(mysql));
    }
    MYSQL_RES* result;
    MYSQL_ROW row;
    result= mysql_store_result(mysql);
    row = mysql_fetch_row(result);
    mysql_free_result(result);  

    if(row != NULL){

        char sql_buf[1024] = {0};
        sprintf(sql_buf,
                "select * from file where hash1 =\"%s\" and hash2 = \"%s\" and path = \"%s\"",
                buf2,buf1,client_path);
        if (mysql_query(mysql, sql_buf)) { 
            printf("%s\n", mysql_error(mysql));
        } 
        MYSQL_RES* result;
        MYSQL_ROW row;
        result= mysql_store_result(mysql);
        row = mysql_fetch_row(result);
        mysql_free_result(result);

        if(row != NULL){
            int file_fd = open(server_path,O_RDWR | O_CREAT, 0600);
            struct stat stat_file;
            fstat(file_fd,&stat_file);
            if(clientfile_len == stat_file.st_size){
                int type_len = 6;
                send(net_fd,&type_len,sizeof(int),MSG_NOSIGNAL);
                send(net_fd,"having",type_len,MSG_NOSIGNAL);
            }else{//同用户进行断点续传
                int type_len = 6;
                send(net_fd,&type_len,sizeof(int),MSG_NOSIGNAL);
                send(net_fd,"resume",type_len,MSG_NOSIGNAL);

                send(net_fd,&stat_file.st_size,sizeof(int),MSG_NOSIGNAL);
                ftruncate(file_fd,clientfile_len);//截断为之前的大小
                int recv_len = clientfile_len - stat_file.st_size;//需要接收的文件>


                char* p = (char*)mmap(NULL,
                                      clientfile_len,
                                      PROT_READ | PROT_WRITE,
                                      MAP_SHARED,
                                      file_fd,
                                      0);

                int recv_ret = recv(net_fd,
                                    p + stat_file.st_size,
                                    recv_len ,
                                    MSG_WAITALL); 
                ERROR_CHECK(recv_ret,0,"recv");

                munmap(p,recv_len);
                if(recv_ret != recv_len){
                    ftruncate(file_fd,recv_ret + stat_file.st_size);
                }

                char sql_buf[1024] = {0};
                sprintf(sql_buf,
                        "update file set flag = 0 where hash1 =\"%s\" and hash2 = \"%s\"",
                        buf2,buf1);
                if (mysql_query(mysql, sql_buf)) {
                    printf("%s\n", mysql_error(mysql));
                }
                //这里更改数据库的传输完成标记为

            }

        }else{
            int type_len = 6;
            send(net_fd,&type_len,sizeof(int),MSG_NOSIGNAL);
            send(net_fd,"failed",type_len,MSG_NOSIGNAL);
        }

    }else{
        char sql_buf[1024] = {0};
        sprintf(sql_buf,
                "select * from file where hash1 =\"%s\" and hash2 =\"%s\"",
                buf2,buf1);
        if (mysql_query(mysql, sql_buf)) {
            printf("%s\n", mysql_error(mysql));
        }
        MYSQL_RES* result;
        MYSQL_ROW row;
        result= mysql_store_result(mysql);
        row = mysql_fetch_row(result);
        mysql_free_result(result);

        if(row == NULL){
            int type_len = 8;
            send(net_fd,&type_len,sizeof(int),MSG_NOSIGNAL);
            send(net_fd,"new_file",type_len,MSG_NOSIGNAL);

            int file_fd = open(server_path,O_RDWR | O_CREAT, 0600);
            ftruncate(file_fd,clientfile_len);
            char* p = (char*)mmap(NULL,
                                  clientfile_len,
                                  PROT_READ|PROT_WRITE,
                                  MAP_SHARED,
                                  file_fd,
                                  0);//注意这里是文件的起始位置同时偏移量必须是4096倍数

            int offset = 0;
            while(offset < clientfile_len){
                int recv_ret = recv(net_fd,
                                    p + offset,
                                    clientfile_len-offset,
                                    0);
                offset += recv_ret;
            }
            munmap(p,clientfile_len);
            
            printf("%s\n",user_path);
            int parent_id = search_pfileid(mysql,user_path);
            if(offset != clientfile_len){//更改标记位为未传输完成
                ftruncate(net_fd,clientfile_len - offset);
                insert_file(mysql,client_path,user_id,parent_id,1,file_name,buf2,buf1,1,0);
            }
            insert_file(mysql,client_path,user_id,parent_id,1,file_name,buf2,buf1,0,0);
        }else{//这里查询到数据库中有相关文件,进行断点续传或者

            int file_fd = open(server_path,O_RDWR,0600);
            struct stat stat_file;
            fstat(file_fd,&stat_file); 

            if(stat_file.st_size == clientfile_len){

                int type_len = 16;
                send(net_fd,&type_len,sizeof(int),MSG_NOSIGNAL);
                send(net_fd,"instand_transfer",type_len,MSG_NOSIGNAL);

                int parent_id = search_pfileid(mysql,user_path);
                insert_file(mysql,client_path,user_id,parent_id,1,file_name,buf2,buf1,0,0);


            }else{//如果不是自己的实现跨用户断点续传

                int type_len = 6;
                send(net_fd,&type_len,sizeof(int),MSG_NOSIGNAL);
                send(net_fd,"resume",type_len,MSG_NOSIGNAL);
                send(net_fd,&stat_file.st_size,sizeof(int),MSG_NOSIGNAL);

                ftruncate(file_fd,clientfile_len);//截断为之前的大小
                int recv_len = clientfile_len - stat_file.st_size;//需要接收>


                char* p = (char*)mmap(NULL,
                                      clientfile_len,
                                      PROT_READ | PROT_WRITE,
                                      MAP_SHARED,
                                      file_fd,
                                      0);

                int recv_ret = recv(net_fd,
                                    p + stat_file.st_size,
                                    recv_len ,
                                    MSG_WAITALL);
                ERROR_CHECK(recv_ret,0,"recv");

                munmap(p,recv_len);
                int parent_id = search_pfileid(mysql,user_path);
                if(recv_ret != recv_len){
                    ftruncate(file_fd,recv_ret + stat_file.st_size);
                    insert_file(mysql,client_path,user_id,parent_id,1,file_name,buf2,buf1,1,0);
                }
                insert_file(mysql,client_path,user_id,parent_id,1,file_name,buf2,buf1,0,0);

            }

        }
    }

return;

}


